//
//  MNDResetPasswordViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/10.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDResetPasswordViewController.h"
#import "UIImage+JKColor.h"

@interface MNDResetPasswordViewController ()

@property (weak, nonatomic) IBOutlet UITextField *oldPasswordTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *comfirmPasswordTextField;
@property (weak, nonatomic) IBOutlet UIButton *submitButton;
@property (weak, nonatomic) IBOutlet UIButton *hideOrShowPwdBtn;

@property (copy, nonatomic) NSString *oldPassword;
@property (copy, nonatomic) NSString *password;
@property (copy, nonatomic) NSString *comfirmPwd;

@end

@implementation MNDResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.submitButton setBackgroundImage:[UIImage imageNamed:@"invest_result_continue_btn"] forState:UIControlStateNormal];
    [self.submitButton setBackgroundImage:[UIImage jk_imageWithColor:kColorLightGray] forState:UIControlStateDisabled];
    
    RAC(self, oldPassword) = self.oldPasswordTextField.rac_textSignal;
    RAC(self, password)    = [RACSignal combineLatest:@[RACObserve(self, passwordTextField.text)] reduce:^id (NSString *pwd){
        return pwd;
    }];
    
    RAC(self, comfirmPwd)  = [RACSignal combineLatest:@[RACObserve(self, comfirmPasswordTextField.text)] reduce:^id (NSString *pwd){
        return pwd;
    }];
    
    __block UITextField *pwdTextField = self.passwordTextField;
    [self.passwordTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 16) {
            pwdTextField.text = [x substringToIndex:16];
        }
    }];
    
    __block UITextField *comfirmTextField = self.comfirmPasswordTextField;
    [self.comfirmPasswordTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 16) {
            comfirmTextField.text = [x substringToIndex:16];
        }
    }];
    
    RAC(self.submitButton, enabled) = [RACSignal combineLatest:@[RACObserve(self, oldPassword),
                                                                 RACObserve(self, password),
                                                                 RACObserve(self, comfirmPwd)]
                                                        reduce:^id(NSString *oldPwd,
                                                                   NSString *pwd,
                                                                   NSString *comfirmPwd){
                                                            
                                                            return @(oldPwd.length > 0 && pwd.length > 0 && comfirmPwd.length > 0);
                                                                 }];
    
    [self.hideOrShowPwdBtn addTarget:self action:@selector(showOrHidePwdAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)showOrHidePwdAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    
    [self.oldPasswordTextField setSecureTextEntry:!sender.isSelected];
    [self.passwordTextField setSecureTextEntry:!sender.isSelected];
    [self.comfirmPasswordTextField setSecureTextEntry:!sender.isSelected];
    
}

- (IBAction)submitAction:(id)sender {
    
    if (self.oldPassword.length <= 0) {
        [NetworkProgressHUD showHudMsg:@"请输入原密码"];
        return;
    }
    if (self.password.length <= 0) {
        [NetworkProgressHUD showHudMsg:@"请输入新密码"];
        return;
    }
    if (self.comfirmPwd.length <= 0) {
        [NetworkProgressHUD showHudMsg:@"请再次输入新密码"];
        return;
    }
    
    if (![self.comfirmPwd isEqualToString:self.password]) {
        [NetworkProgressHUD showHudMsg:@"两次输入的密码不一致"];
        return;
    }
    
    NSDictionary *params = [MNDSign signedParamWithDict:@{@"user_login_password"    : self.oldPassword,
                                                          @"new_login_password"     : self.password,
                                                          @"confirm_login_password" : self.comfirmPwd}];
    
    WeakSelf(self);
    [NetworkProgressHUD showHud];
    
    [APIManager sendRequestWithPath:API_LODIN_PASSWORD_UPDATE parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD showHudMsg:@"密码修改成功"];
            
            [weakself performSelector:@selector(backAction) withObject:nil afterDelay:2.0];
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
